package com.sxpi.user.service.user;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sxpi.common.result.R;
import com.sxpi.user.service.weixin.WeixinLoginService;
import com.sxpi.user.mapper.UserMapper;
import com.sxpi.user.pojo.User;
import com.sxpi.user.dto.LoginTeDto;
import com.sxpi.user.dto.LoginDto;
import lombok.extern.slf4j.Slf4j;
import org.h2.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: 董伟豪
 * @Date: 2022/10/3 15:24
 */
@Service
@Slf4j
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private WeixinLoginService weixinLoginService;

    /**
     * 登录
     * @param loginVo
     * @return
     */
    @Override
    public R login(LoginDto loginVo) {
        // 获取openId
        String openId = weixinLoginService.getOpenId(loginVo.getCode());
        if(StringUtils.isNullOrEmpty(openId)){
            return R.error("解析openId失败...");
        }
        // 根据openId查询用户是否注册
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(User::getOpenid,openId);
        User dbUser = this.getOne(userLambdaQueryWrapper);
        if(dbUser == null){
            dbUser = new User();

            dbUser.setOpenid(openId);
            dbUser.setUnionid(openId);
            // 数据库的编码一定 要使用utf8mb4 否则如果用户微信的昵称是一个图片或者符号就会保存失败
            dbUser.setNickname(loginVo.getNickName());
            dbUser.setUsername(loginVo.getNickName());
            dbUser.setAvatar(loginVo.getAvatarUrl());

            dbUser.setSign("Ta很懒，什么都没有留下~~~");
            dbUser.setSex(1);
            dbUser.setTelephone("");
            dbUser.setVip(1);
            dbUser.setCron(50);
            dbUser.setStudydays(1);
            dbUser.setIsDelete(0);
            dbUser.setForbbiden(0);
            dbUser.setRole("normal");
            dbUser.setPassword("");
            dbUser.setFans(0);
            dbUser.setGznums(0);
            dbUser.setArchive(0);

            // 进行注册
            this.save(dbUser);
        }
        return R.ok(BeanUtil.beanToMap(dbUser));
    }

    /**
     * 更新手机号码更新信息
     * @param loginTeVo
     * @return
     */
    @Override
    public boolean updateTel(LoginTeDto loginTeVo) {
        String tel = weixinLoginService.getTel(loginTeVo.getCode());
        if(!StringUtils.isNullOrEmpty(tel)){
            LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
            userLambdaQueryWrapper.eq(User::getTelephone,tel);
            User user = this.getOne(userLambdaQueryWrapper);
            if(user == null){
                User updateUser = new User();
                updateUser.setId(loginTeVo.getUserId());
                updateUser.setTelephone(tel);
                this.updateById(updateUser);
                return true;
            }
        }
        return false;
    }
}
